﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using SimpleConfig;

namespace GEDownloader
{
    /// <summary>
    /// Options main form. At the moment, only for proxy settings.
    /// </summary>
    public partial class frmOptions : Form {
        /// <summary>
        /// Workaround for having access to frmMain.Config and frmMain.reloadSettings()
        /// </summary>
        private frmMain  _parent;

        /// <summary>
        /// Class Form Constructor
        /// </summary>
        /// <param name="parent">instance of frmMain</param>
        public frmOptions(frmMain parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        /// <summary>
        /// Cancel button click event. Closes the form without saving anything.
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event properties</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Changes a property value
        /// </summary>
        /// <param name="section">Section where the property is stored</param>
        /// <param name="property">Name of the property to change</param>
        /// <param name="value">Property value to set in string format</param>
        private void cfgSetValue(string section, string property, string value)
        {
            this._parent.Config.writeString(value, property, section);
        }
        
        /// <summary>
        /// Reads a property value
        /// </summary>
        /// <param name="section">Section where the property is stored</param>
        /// <param name="property">Name of the property to change</param>
        /// <returns>Value of the property in string format</returns>
        private string cfgGetValue(string section, string property)
        {
            return this._parent.Config.readString(property, section);
        }

        /// <summary>
        /// Accept button click event. Saves all properties, 
        /// reload the parent config object and closes the form.
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event properties</param>
        private void btnAccept_Click(object sender, EventArgs e)
        {
            cfgSetValue("proxy", "hostname", txtProxyHost.Text);
            cfgSetValue("proxy", "use_proxy", checkUseProxy.Checked.ToString());
            cfgSetValue("proxy", "port", txtProxyPort.Text);
            cfgSetValue("proxy", "username", txtProxyUser.Text);
            cfgSetValue("proxy", "password", txtProxyPassword.Text);
            if (comboProxyType.SelectedItem.ToString() == "")
                cfgSetValue("proxy", "type", "No proxy");
            else
                cfgSetValue("proxy", "type", comboProxyType.SelectedItem.ToString());
            this._parent.Config.saveConfig();
            this._parent.reloadConfig();
            this.Close();
        }

        /// <summary>
        /// FrmOptions Load event. Loads all the properties to their corresponding controls.
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event properties</param>
        private void frmOptions_Load(object sender, EventArgs e)
        {

            checkUseProxy.Checked = (bool)(new BooleanConverter()).ConvertFromString(cfgGetValue("proxy", "use_proxy"));
            String typeValue = cfgGetValue("proxy", "type");
            comboProxyType.SelectedItem = (typeValue == "") ? "No Proxy" : typeValue;
            txtProxyHost.Text = cfgGetValue("proxy", "hostname");
            txtProxyPort.Text = cfgGetValue("proxy", "port");
            txtProxyUser.Text = cfgGetValue("proxy", "username");
            txtProxyPassword.Text = cfgGetValue("proxy", "password");
        }
    }
}
